﻿@{
    Layout = "~/Modules/LayoutEngine/Views/Shared/_LayoutAdmin.cshtml";
    Style.Reqiured("List").AtHead();
    Script.Reqiured("List").AtHead();
    ViewBag.Title = "选择字段";
}

<script>

    $(function () {
        var _open = window.parent;
        var userId = $(_open.parent.document).find("input[id='hdnCurrentUserId']").val();//'4a15860d-780e-4511-a558-9b857c1bf1f6';
        // 加载应用List
        $.ajax({
            type: "POST",
            url: "@Urls.ApiHost/api/mp/list/getappbyuserId?t=" + new Date().getMilliseconds(),
            data: {
                "": userId
            },
            success: function (result) {
                if (result && result.length > 0) {
                    var html = "";
                    $("#applist").empty();
                    for (var i = 0 ; i < result.length; i++) {
                        var item = result[i];
                        if (i == 0) {
                            loadTables(item["Id"]);
                        }

                        var icon = item["ImgClass"];
                        if (!icon) {
                            icon = "menu-icon glyphicon glyphicon-tasks";
                        }

                        html += "<li class='dd-item dd2-content' style='padding:0;'>";
                        html += '<a href="#" onclick="loadTables(\'' + item["Id"] + '\',\'' + userId + '\');" class="mymenu" >';
                        html += '<i class="menu-icon ' + icon + '"></i><span class="menu-text"> ' + item["AppSortName"] + ' </span>';
                        html += '</a>';
                        html += '</li>';
                    }
                    $("#applist").append(html);
                }
            }
        });
    });

    function loadTables(appId) {
        var name = $("#txtTableSearch").val();
            //加载表数据
            var d = [{ "Id": "", "opt": "select", "name": "DataManage" },
                { "item": { "Name": "appid", "Operation": "eq", "Value": appId, "ValueType": "string" } }];
            doajax("@Urls.ApiHost/api/mp/list/find", d, function (b) {
                if (d) {
                    $("#loading").removeClass("loading");
                    $("#loading").addClass("loading hide");
                    b = JSON.parse(b);
                    b = JSON.parse(b.Message);
                    if (b.length != 0) {
                        $("#table").show();
                        $("#noFormData").hide();
                        $("#table .bs-glyphicons-list").empty();
                        if (name) {
                            $("#field").hide();
                            $("#listTitle").hide();
                        }
                        $.each(b, function (i, k) {
                            if (name) {
                                if (k.data_table.toLowerCase().indexOf(name.toLowerCase()) != -1) {
                                    $("#table .bs-glyphicons-list").append("<li style=\"width:120px;height:110px;cursor:pointer;background-color:#DBDBDB\" onmouseover=\"this.style.backgroundColor='#C2C2C2'\" onmouseout=\"this.style.backgroundColor='#DBDBDB'\" table=" + k.data_table + "> <span class=" + k._id.$oid + " >" + k.data_table + "</span></li>");
                                }
                            } else {
                                $("#table .bs-glyphicons-list").append("<li style=\"width:120px;height:110px;cursor:pointer;background-color:#DBDBDB\" onmouseover=\"this.style.backgroundColor='#C2C2C2'\" onmouseout=\"this.style.backgroundColor='#DBDBDB'\" table=" + k.data_table + "> <span class=" + k._id.$oid + " >" + k.data_table + "</span></li>");
                            }
                        })
                    } else {

                        $("#table").hide();
                        $("#noFormData").show();
                    }

                    //添加表单点击事件
                    $("#table .bs-glyphicons-list li").each(function (i, k) {
                        $(k).attr("onclick", "loadField(this)");
                        if ('@ViewBag.FormCode' == $(k).attr("table")) {
                            $(k).click(loadField(this));
                        }
                    })
                }

            });

        }

        curObj = null;
        function loadField(k) {
            name = $("#txtFieldSearch").val();
            curObj = $(k);
            //获取数据表名称
            var data_table = $(k).find("span:eq(0)").html();
            $("#table .bs-glyphicons-list li").each(function (i, o) {

                if ($(o).find("span:eq(0)").html() == data_table) {
                    $(o).css('background-color', "#919191");
                    $(o).attr("onmouseover", "");
                    $(o).attr("onmouseout", "");
                } else {
                    $(o).css('background-color', "#DBDBDB");
                    $(o).attr("onmouseover", "this.style.backgroundColor='#C2C2C2'");
                    $(o).attr("onmouseout", "this.style.backgroundColor='#DBDBDB'");
                }

            })

            //加载列表数据
            var d = [{ "Id": "", "opt": "select", "name": "DataManage" }, { "item": { "Name": "data_table", "Operation": "eq", "Value": data_table, "ValueType": "string" } }];
            doajax("@Urls.ApiHost/api/mp/list/findfirst", d, function (b) {
                if (b) {
                    b = JSON.parse(b);
                    b = JSON.parse(b.data_field);
                    if (b.length != 0) {
                        $("#field").show();
                        $("#listTitle").show();
                        $("#noListData").hide();
                        $("#field .bs-glyphicons-list").empty();
                        $.each(b, function (i, k) {
                            var datatype = "";
                            if (k.datatype) {
                                datatype = k.datatype;
                            }
                            if (!datatype) {
                                datatype = "undefine";
                            }
                            var datatypetitle = "";
                            var datatypeU = datatype.toUpperCase();
                            if (datatypeU.indexOf("DECIMAL") != -1 ||
                                datatypeU.indexOf("INT") != -1||
                                datatypeU.indexOf("FLOAT") != -1 ||
                                datatypeU.indexOf("REAL") != -1 ||
                                datatypeU.indexOf("DOUBLE") != -1) {
                                datatypetitle = "数字类型";
                            }
                            else if (datatypeU.indexOf("DATETIME") != -1) {
                                datatypetitle = "日期类型";
                            }
                            else if (datatypeU.indexOf("VARCHAR") != -1 ||
                                datatypeU.indexOf("NVARCHAR") != -1) {
                                datatypetitle = "文本类型";
                            }
                            else {
                                datatypetitle = "无效类型";
                            }
                            var ftitle = k.field + "(" + k.desc + "，" + datatypetitle + ")";
                            if (name) {
                                if (k.field.toLowerCase().indexOf(name.toLowerCase()) != -1) {
                                    $("#field .bs-glyphicons-list").append("<li style=\"width:120px;height:110px;cursor:pointer;background-color:#DBDBDB\" onmouseover=\"this.style.backgroundColor='#C2C2C2'\" onmouseout=\"this.style.backgroundColor='#DBDBDB'\"> <span class='" + data_table + "' desc='" + k.desc + "' ftype='" + datatype + "' field='" + k.field + "'>" + ftitle + "</span></li>");
                                }
                            } else {
                                $("#field .bs-glyphicons-list").append("<li style=\"width:120px;height:110px;cursor:pointer;background-color:#DBDBDB\" onmouseover=\"this.style.backgroundColor='#C2C2C2'\" onmouseout=\"this.style.backgroundColor='#DBDBDB'\"> <span class='" + data_table + "' desc='" + k.desc + "' ftype='" + datatype + "' field='" + k.field + "'>" + ftitle + "</span></li>");
                            }
                        })
                    } else {
                        $("#field").hide();
                        $("#listTitle").show();
                        $("#noListData").show();
                    }
                    //添加表单点击事件
                    $("#field .bs-glyphicons-list li").each(function (i, k) {
                        $(k).attr("onclick", "setCode(this)");
                    })
                }
            })
        }


        function setCode(k) {
            // 获取field
            var formCode = $(k).find("span:eq(0)").attr("class");
            var field = $(k).find("span:eq(0)").attr("field");
           
            // 赋值，关闭弹出框
            var _open = window.parent;
            var setField = '@ViewBag.Field';

            $(_open.document).find("input[id='FormCode']").val(formCode);
            $(_open.document).find("input[id='" + setField + "']").val(field);
            $(_open.document).find("div[class='CloseWindow']").click();
        }

</script>

<div class="page-body">
    <div class="row">
        <input type="hidden" id="hdnAppId"/>
        <div class="col-lg-12 col-sm-12 col-xs-12">
            <div class="row">
                <div class="col-lg-2 col-sm-2 col-xs-12">
                    <div class="widget">
                        <div class="widget-header bg-info">
                            <span class="widget-caption">
                                <a class="notdrag" style="color:#fff">应用列表</a>
                            </span>
                        </div>

                        <div class="widget-body" style="height:426px;">
                            <div class="page-container">
                                <div id="sidebar" style="overflow:auto; max-width:180px;">
                                    <ul id="applist" class="nav sidebar-menu">
                                        @*<li class="myli">
                                        <a href="#" onclick="rediectUrl(this);" class="mymenu" o-href="/CMS/admin/Layout">
                                            <i class="menu-icon glyphicon glyphicon-align-justify"></i><span class="menu-text">@Html.Lang("布局列表") </span>
                                        </a>
                                </li>*@
                                    </ul>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="col-lg-10 col-sm-10 col-xs-12">
                    <div class="widget">
                        <div class="widget-body" style="width:530px;height:460px;">
                            <div style="height:437px;overflow-y:auto;">
                                <div style=" margin-bottom: 5px;">
                                    <span style="width:100%;font-size:13px;font-weight:bold;">数据表(点击表名称后选择相应字段)</span>
                                    <span class="search">
                                        <input id="txtTableSearch" class="text_div" onkeyup="loadTables()" title="输入名称搜索" placeholder="输入名称搜索">
                                    </span>
                                </div>
                                <div id="noFormData" style="display:none;">
                                    <span style="color:red;width:100%">暂无表数据！</span>
                                </div>
                                <div id="table" class="bs-glyphicons">
                                    <ul class="bs-glyphicons-list"></ul>
                                </div>
                                <div id="listTitle" style="display:none; margin-top:20px;margin-bottom:5px;">
                                    <span style="width:100%;font-size:13px;font-weight:bold;">字段</span>
                                    <span class="search">
                                        <input id="txtFieldSearch" class="text_div" onkeyup="loadField(curObj)" title="输入名称搜索" placeholder="输入名称搜索">
                                    </span>
                                </div>
                                <div id="noListData" style="display:none;">
                                    <span style="color:red;width:100%">暂无字段数据！</span>
                                </div>
                                <div id="field" class="bs-glyphicons" style="display:none">
                                    <ul class="bs-glyphicons-list"></ul>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>


